// 云函数入口文件（适配uniCloud）
'use strict';
exports.main = async (event, context) => {
  try {
    const db = uniCloud.database();
    const speciesColl = db.collection('species');

    const {
      species_name
    } = event;

    if (!species_name) {
      return {
        code: 400,
        message: '缺少物种名称'
      }
    }

    const res = await speciesColl.where({
      name: species_name
    }).limit(1).get()

    if (!res.data) {
      return {
        code: 404,
        message: '未找到该物种记录'
      }
    }

    return {
      code: 200,
      data: {
        ...res.data,
      }
    };
  } catch (err) {
    console.error('云函数错误:', err);
    return {
      code: 500,
      message: '服务器内部错误'
    };
  }
}